<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Admin_user;
use Validator;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use DB;
use Illuminate\Database\Eloquent\Model;
use App\Http\Model\Goods;

class GoodsController extends CommonController
{
    public function upload()
    {
//        dd(Input::all());
        $file = Input::file('file_upload');
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;
            $path = $file->move(public_path().'/uploads',$newName);
            $filepath = 'uploads/'.$newName;
            return  $filepath;
        }
    }
    //分页浏览信息
    public function index(Request $request)
    {
       //获取所有的商品
        $list = DB::table("goods")->paginate(5);
        $data = DB::table("category")->get();
        $arr = [1=>"新上架",2=>"出售",3=>"已下架"];
//        给模板传变量的三种方法
        return view('admin.goods.index',compact("list","data","arr"));
    }
    
   
    
    //添加表单
    public function create()
    {
        $list = \DB::table("category")->get();
        $list = $this->findSubTree($list,0,0);
        return view("admin.goods.add",compact("list"));
    }
    //执行添加
    public function store(Request $request)
    {
        //获取所需信息
        //$input = Input::except('_token');
       // dd($request->all());
        $input = $request->except('_token','file_upload');
        $input['created_at'] = date('Y-m-d h:i:s',time());
        $re =  Goods::create($input);
        if($re){
            return redirect('admin/goods');
        }else{
            return back()->with('errors','商品添加失败');
        }
    }

    public function show($id)
    {
        //
    }
    
    //执行删除
    public function destroy($id)
    {
        
        $re =  Goods::where('id',$id)->delete();

        
      if($re){
          $data = [
              'status'=>0,
              'msg'=>'商品删除成功'
          ];
      }else{
          $data = [
              'status'=>1,
              'msg'=>'商品删除失败'
          ];
      }
      return $data;
    }
    
    //加载修改表单
    public function edit($id)
    {
        $list = DB::table("category")->get();
        $data = Goods::find($id);
       //可以使用compact函数给模板传变量
       return view('admin.goods.edit',compact('data','list'));
    }
    
    //执行修改
    public function update($id,Request $request)
    {
        $input = Input::except('_token','_method','file_upload');
        $art = Goods::find($id);
        $re =  $art->update($input);
        if($re){
            return redirect('admin/goods');
        }else{
            return back()->with('errors','商品修改失败');
        }
        
    }
    

    /*
    //为当前用户准备分配角色信息
    public function loadRole($uid=0)
    {
        //获取所有角色信息
        $rolelist = DB::table("role")->get();
        //获取当前用户的角色id
        $rids = DB::table("user_role")->where("uid",$uid)->lists("rid");
        //加载模板
        return view("admin.users.rolelist",["uid"=>$uid,"rolelist"=>$rolelist,"rids"=>$rids]);
    }
    
    public function saveRole(Request $request){
        $uid = $request->input("uid");
        //清除数据
        \DB::table("user_role")->where("uid",$uid)->delete();
        
        $rids = $request->input("rids");
        if(!empty($rids)){
            //处理添加数据
            $data = [];
            foreach($rids as $v){
                $data[] = ["uid"=>$uid,"rid"=>$v];
            }
            //添加数据
            DB::table("user_role")->insert($data);
        }
        return "角色保存成功!";
    }
    */
    protected function findSubTree($cates,$cate_id=0,$level=1){
        $subtree = [];//子孙数组
        foreach ($cates as $v) {
            if($v->cate_pid==$cate_id){
                $v->level = $level;
                $subtree[] = $v;
                $subtree = array_merge($subtree,$this->findSubTree($cates,$v->cate_id,$level+1));
            }
        }
        return $subtree;
    }


}
